<template>
  <transition
    name="animate__animated animate__bounce"
    appear
    enter-active-class="animate__backInDown"
    leave-active-class="animate__zoomOut"
  >
    <div id="root">
      <div class="todo-container">
        <div class="todo-wrap">
          <MyHeader @addTodo="addTodo" />
          <List :todos="todos" />
          <MyFooter
            :todos="todos"
            @checkAllTodo="checkAllTodo"
            @clearAllTodo="clearAllTodo"
          />
        </div>
      </div>
      <Tablepet />
    </div>
  </transition>
</template>

<script>
// VM中以 $开头的属性 都是框架保留使用的特殊属性
import pubsub from "pubsub-js";
import MyHeader from "../components/todolist/MyHeader.vue";
import List from "../components/todolist/List.vue";
import MyFooter from "../components/todolist/MyFooter.vue";
import Tablepet from "./Tablepet.vue";

export default {
  name: "App",
  components: {
    MyHeader,
    List,
    MyFooter,
    Tablepet,
  },
  data() {
    return {
      todos: JSON.parse(localStorage.getItem("todos")) || [],
    };
  },
  methods: {
    //添加一个todo
    addTodo(todoObj) {
      //在最前面添加数据
      this.todos.unshift(todoObj);
    },

    //勾选或者取消勾选一个todo
    checkTodo(id) {
      this.todos.forEach((todos) => {
        if (todos.id === id) {
          todos.done = !todos.done;
        }
      });
    },

    //删除一个todo项
    removeTodo(_, id) {
      this.todos = this.todos.filter((todos) => {
        // filter. 为true才会被保留，为false会被过滤
        //只有todos.id !== id，才会会被保留，todos.id === id，则会被过滤
        return todos.id !== id;
      });
    },

    //全选或者全不选
    checkAllTodo(done) {
      this.todos.forEach((todos) => {
        todos.done = done;
      });
    },

    //清除所有已经完成的任务
    clearAllTodo() {
      this.todos = this.todos.filter((todos) => {
        // done值为真的过滤
        // filter. 为true才会被保留，为false会被过滤
        return !todos.done;
      });
    },

    //修改并保存
    updateTodo(id, title) {
      this.todos.forEach((todos) => {
        if (todos.id === id) {
          todos.title = title;
        }
      });
    },
  },
  watch: {
    todos: {
      deep: true,
      handler(value) {
        localStorage.setItem("todos", JSON.stringify(value));
      },
    },
  },
  mounted() {
    this.$bus.$on("checkTodo", this.checkTodo);
    this.pubId = pubsub.subscribe("removeTodo", this.removeTodo);
    this.$bus.$on("updateTodo", this.updateTodo);
  },

  beforeDestroy() {
    this.$bus.$off("checkTodo");
    pubsub.unsubscribe(this.pubId);
    this.$bus.$off("updateTodo");
  },
};
</script>

<style>
/*base*/

.btn {
  display: inline-block;
  padding: 4px 12px;
  margin-bottom: 0;
  font-size: 14px;
  line-height: 20px;
  text-align: center;
  vertical-align: middle;
  cursor: pointer;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.2),
    0 1px 2px rgba(0, 0, 0, 0.05);
  border-radius: 4px;
}

.btn-danger {
  color: #fff;
  background-color: #da4f49;
  border: 1px solid #bd362f;
}

.btn-danger:hover {
  color: #fff;
  background-color: #bd362f;
}

.btn-edit {
  color: #fff;
  background-color: green;
  border: 1px solid rgb(62, 143, 62);
}

.btn-edit:hover {
  color: #fff;
  background-color: rgb(31, 111, 31);
}

.btn:focus {
  outline: none;
}

.todo-container {
  width: 600px;
  margin: 0 auto;
}

.todo-container .todo-wrap {
  padding: 10px;
  border: 1px solid #ddd;
  border-radius: 5px;
}
</style>
